package com.sy.servlet;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

@WebServlet("/getFile")
public class GetFileServlet extends HttpServlet {
    private static final String SAVE_DIR = "d:\\imgs";

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String fileName = req.getParameter("fileName");
        try (FileInputStream fis = new FileInputStream(SAVE_DIR + File.separator + fileName);
             OutputStream os = res.getOutputStream()) {
            IOUtils.copy(fis, os);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
